package com.mywang.weitravel.modules.login;

import com.mywang.weitravel.base.BasePresenter;
import com.mywang.weitravel.model.UserBean;
import com.mywang.weitravel.net.RetrofitHelper;
import com.mywang.weitravel.net.RxSchedulers;
import com.mywang.weitravel.net.VObserver;
import com.mywang.weitravel.util.EncodeUtils;

import java.util.HashMap;

/**
 * Created by wangmingyun on 2019/12/5.
 */
public class LoginPresenter extends BasePresenter<LoginContract.View> implements LoginContract.Presenter {

    public LoginPresenter(LoginContract.View view) {
        super(view);
    }

    @Override
    public void doLogin(String username, String password) {
        mView.showLoading();
        HashMap<String, String> params = new HashMap<>();
        params.put("username", username);
        params.put("password", EncodeUtils.md5(password));

        RetrofitHelper.getInstance().getServer()
                .doLogin(params)
                .compose(RxSchedulers.applySchedulers())
                .as(bindToLife())
                .subscribe(new VObserver<UserBean>() {
                    @Override
                    protected void onSuccess(UserBean userBean) {
                        mView.loginSuccess(userBean);
                        mView.hideLoading();
                    }

                    @Override
                    protected void onFailure(int code, String message, boolean isShowMsg) {
                        super.onFailure(code, message, isShowMsg);
                        mView.hideLoading();
                    }
                });
    }
}
